﻿using Bids.Core.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Telerik.OpenAccess;
using Telerik.OpenAccess.Metadata;
using Telerik.OpenAccess.Metadata.Fluent;

namespace Bids.Core.DomainObjects
{
	/// <summary>
	/// 任务列表
	/// </summary>
	[Serializable]
	public class Task : IEntity
	{
        public Task()
        {
            this.Styles = new List<Style>();
            this.UsedFor = new List<UsedFor>();
            this.Contributions = new List<Contribution>();
            this.TaskPays = new List<TaskPay>();
        }
		/// <summary>
		/// 
		/// </summary>
        public virtual int ID { set; get; }
		/// <summary>
		/// 行业
		/// </summary>
        public virtual int TradeId { set; get; }
		/// <summary>
		/// 客户对象
		/// </summary>
        public virtual string Custom { set; get; }
        /// <summary>
        /// 企业理念
        /// </summary>
        public virtual string BusinessPhilosophy { get; set; }
		/// <summary>
		/// 特征
		/// </summary>
        public virtual string Feature { set; get; }
		/// <summary>
		/// 公司名称
		/// </summary>
        public virtual string Company { set; get; }
		/// <summary>
		/// 口号
		/// </summary>
        public virtual string Slogan { set; get; }
		/// <summary>
		/// 名称
		/// </summary>
        public virtual string Name { set; get; }
		/// <summary>
		/// 包含的字母或数字
		/// </summary>
        public virtual string LettersOrNumbers { set; get; }
		/// <summary>
		/// 包含的文字
		/// </summary>
        public virtual string Characters { set; get; }
		/// <summary>
		/// 喜欢的颜色
		/// </summary>
        public virtual string GoodColor { set; get; }
		/// <summary>
		/// 不喜欢的颜色
		/// </summary>
        public virtual string BadColor { set; get; }
		/// <summary>
		/// 额外信息
		/// </summary>
        public virtual string Extraneous { set; get; }
        /// <summary>
        /// 额外信息文件
        /// </summary>
        public virtual string ExtraneousFile { get; set; }
        /// <summary>
        /// 表达的价值观
        /// </summary>
        public virtual string Expression { get;set;}
		/// <summary>
		/// 雇主id
		/// </summary>
        public virtual int EmployerId { set; get; }
        /// <summary>
        /// 设计师Id
        /// </summary>
        public virtual int? DesignerId { get; set; }
		/// <summary>
		/// 状态
		/// </summary>
        public virtual int Status { set; get; }
		/// <summary>
		/// 创建时间
		/// </summary>
        public virtual DateTime CreateTime { set; get; }
		/// <summary>
		/// 是否保密
		/// </summary>
        public virtual bool KeepSecret { set; get; }
		/// <summary>
		/// 费用
		/// </summary>
        public virtual int Cost { set; get; }
        /// <summary>
        /// 预付款
        /// </summary>
        public virtual int Imprest { get; set; }
		/// <summary>
		/// 支付订单号
		/// </summary>
        public virtual string PayOrderCode { set; get; }
        /// <summary>
        /// 完成时间
        /// </summary>
        public virtual DateTime? FinishTime { get; set; }
        /// <summary>
        /// 套餐Id
        /// </summary>
        public virtual int SetMealId { get; set; }
        /// <summary>
        /// 支付状态
        /// </summary>
        public virtual int PayState { get; set; }

        public virtual TaskPayStatus TaskPayState
        {
            get
            {
                return (TaskPayStatus)this.PayState;
            }
        }
        public virtual TaskStatus TaskState 
        {
            get 
            {
                return (TaskStatus)this.Status;
            }
        }
        public virtual IList<Contribution> Contributions { get; set; } 
        public virtual IList<Style> Styles { get; set; }
        public virtual IList<UsedFor> UsedFor { get; set; }
        public virtual IList<TaskPay> TaskPays { get; set; }
        public virtual IList<Discussion> Discussionss { get; set; }
        
        public virtual User Employer { get; set; }
        public virtual User Designer { get; set; }
        public virtual Trade Trade { get; set; }
        public virtual SetMeal SetMeal { get; set; }

        public virtual bool CanSelectEnter
        {
            get
            {
                if (this.TaskState == TaskStatus.选稿中)
                {
                    if (this.Contributions.Where(t => t.ContributionStatus == ContributionStatus.修稿中).Count() ==0)
                        return true;
                }
                return false;
            }
        }


        public static MappingConfiguration<Task> CreateConfiguration()
        {
            var threadMapping = new MappingConfiguration<Task>();
            threadMapping.MapType(t => new
            {
                t.ID,
                t.TradeId,
                t.Custom,
                t.BusinessPhilosophy,
                t.Feature,
                t.Company,
                t.Slogan,
                t.Name,
                t.LettersOrNumbers,
                t.Characters,
                t.GoodColor,
                t.BadColor,
                t.Extraneous,
                t.ExtraneousFile,
                t.Expression,
                t.EmployerId,
                t.DesignerId,
                t.Status,
                t.CreateTime,
                t.KeepSecret,
                t.Cost,
                t.Imprest,
                t.PayOrderCode,
                t.FinishTime,
                t.SetMealId,
                t.PayState
            }).ToTable("Tasks");
            threadMapping.HasProperty(t => t.ID).IsIdentity(KeyGenerator.Autoinc);
            threadMapping.HasAssociation(t => t.Contributions).WithOpposite(d=>d.Task).HasConstraint((t,d)=>t.ID == d.TaskId);
            threadMapping.HasAssociation(t => t.TaskPays).WithOpposite(d => d.Task).HasConstraint((t,d)=>t.ID == d.TaskId);
            threadMapping.HasAssociation(t => t.Designer).HasConstraint((t, d) => t.DesignerId == d.ID);
            return threadMapping;
        }

        public string GetRemainingTime()
        {
            System sys = (System)HttpContext.Current.Application["System"];
            int daysOfSubmit = sys.DaysOfSubmit;
            int daysOfChoose = sys.DaysOfChoose;
            int daysOfModify = sys.DaysOfModify;
            TimeSpan dateDiff = new TimeSpan();
            DateTime now = DateTime.Now;
            switch (this.TaskState)
            { 
                case TaskStatus.投稿中:
                    dateDiff = this.CreateTime.AddDays(daysOfSubmit) - now;
                    break;
                case TaskStatus.选稿中:
                case TaskStatus.稿件确认:
                    dateDiff = this.CreateTime.AddDays(daysOfModify + daysOfChoose) - now;
                    break;
                case TaskStatus.已结束:
                    return "已完成";
            }
           return string.Format("{0}天{1:00}时{2:00}分", dateDiff.Days,dateDiff.Hours ,dateDiff.Minutes);
        }
	}
    public enum TaskStatus
    { 
        未付款 = 0,
        投稿中 =1 ,
        选稿中 = 2,
        稿件确认 =3,
        已结束 =4
    }
    public enum TaskPayStatus
    { 
        未付款 = 0,
        已付预付款 = 1,
        已付全款 = 2
    }
}

